'''
    列表推导式
    实现list中每一项的平方
    set 也可以被推导
    dict 也可以被推导
    元组 也可以被推导
'''

a1 = [1,2,3,4,5,6,7,8] # list类型
a2 = {1,2,3,4,5,6,7,8} # 集合类型
a3 = (1,2,3,4,5,6,7,8) # 元组类型

b = [ i**2 for i in a1] # 每一项
c = [ i**2 for i in a1 if i>=5] # 第5项以后
d = { i**2 for i in a2}
e = ( i**2 for i in a3)

print(b)
print(c)
print(d)
print(e)

print('--------------')
print('推导dict')

students = {
    '小明': 18,
    '小红': 19,
    '小花': 20
}

s_key = [key for key,value in students.items()] # 打印key
s_value = [value for key,value in students.items()] # 打印value
s_list = {value:key for key,value in students.items()} # key和value颠倒

print(s_key)
print(s_value)
print(s_list)
